package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/7/29 9:13
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Controller
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("/f1")
    public void f1(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("ParamController.f1");
        ServletContext application1 = request.getServletContext();
        ServletContext application2 = session.getServletContext();
        // 在Handler方法中,想要获取ServletContext
        // 可以通过request或者session进行获取
    }

    // ServletContext不能直接作为Handler方法参数
    @RequestMapping("/f2")
    public void f2(ServletContext application){
        System.out.println("ParamController.f2");
    }

    @RequestMapping("/f3")
    public void f3(InputStream in, OutputStream out){
        System.out.println("ParamController.f3");
    }

    @RequestMapping("/f4")
    public void f4(Reader reader, Writer writer){
        System.out.println("ParamController.f4");
    }

    @RequestMapping("/f5")
    public void f5(InputStream in,Reader reader){
        System.out.println("ParamController.f5");
    }

    @RequestMapping("/f6")
    public String f6(Model model, Map map, ModelMap modelMap){
        model.addAttribute("model","Hello Model");
        map.put("map","Hello Map");
        // ModelMap既可以使用Model的方法,也可以使用Map的方法
        modelMap.addAttribute("modelMap1","Hello ModelMap-Model");
        modelMap.put("modelMap2","Hello ModelMap-Map");
//        return "scope";
        return "redirect:/showScope";
        // 此时重定向之后,作用域中数据丢失
        // 但是地址栏发生了改变,多了如下内容
        // ?model=Hello+Model
        // map=Hello+Map
        // modelMap1=Hello+ModelMap-Model
        // modelMap2=Hello+ModelMap-Map
        // 可以发现,丢失的数据全部在地址栏体现出来了
        // 改为?传参的方式进行存储了
    }

    @RequestMapping("/f7")
    public String f7(HttpServletRequest request){
        request.setAttribute("msg","Hello request");
        return "redirect:/showScope";
    }

    @RequestMapping("/f8")
    public void f8(User user){
        System.out.println(user);
    }

    @RequestMapping("/f9")
    public void f9(Integer id,String username,String password,String phone,String address){
        User user = new User(id,username,password,phone,address);
        System.out.println(user);
    }
}
